﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Time.Utility
{
    public class LogHelper
    {
        public static void PrintLog(string message)
        {
            try
            {
                DateTime timeN = DateTime.Now;
                string directory = "d:\\MES_Logs\\" + timeN.ToLongDateString();
                //string delDir = "d:\\MES_Logs\\" + timeN.AddMonths(-3).ToLongDateString();
                //生成文件夹
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                ////删除文件夹。第二个参数选true，表示删除该目录的子目录及子文件；如果选false，则该目录不为空时不能删除
                //if (Directory.Exists(delDir))
                //{
                //    Directory.Delete(delDir, true);
                //}
                File.AppendAllText(directory + "\\" + timeN.ToString("yyyyMMddHH") + ".log", "************************" +
                    timeN.ToString("yyyy-MM-dd HH:mm:ss") + "***************************\r\n" + message + "\r\n");
            }
            catch (Exception ex)
            {
                PrintLog(ex.ToString());
            }
        }

        public static void PrintLog(LogType type,string message)
        {
            try
            {
                DateTime timeN = DateTime.Now;
                string directory=string.Empty;
                switch (type)
                {
                    case LogType.OfflineData:
                        directory = "d:\\MES_Data\\离线数据";
                        break;
                    case LogType.RunRecord:
                        directory = "d:\\MES_Logs\\运行记录";
                        break;
                    default:
                        directory = "d:\\MES_Logs\\";
                        break;

                }
                directory = directory + timeN.ToLongDateString();
             
                //生成文件夹
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
            
                File.AppendAllText(directory + "\\" + timeN.ToString("yyyyMMddHH") + ".log", "************************" +
                    timeN.ToString("yyyy-MM-dd HH:mm:ss") + "***************************\r\n" + message + "\r\n");
            }
            catch (Exception ex)
            {
                PrintLog(ex.ToString());
            }
        }

        public static void PrintInsertLog(string message, string PathDir = "d:/MES_Insert_Logs/")
        {
            try
            {
                DateTime timeN = DateTime.Now;
                string directory = "d:\\MES_Logs\\" + timeN.ToLongDateString();
                string delDir = PathDir + timeN.AddMonths(-3).ToLongDateString();
                //生成文件夹
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                //删除文件夹。第二个参数选true，表示删除该目录的子目录及子文件；如果选false，则该目录不为空时不能删除
                if (Directory.Exists(delDir))
                {
                    Directory.Delete(delDir, true);
                }
                File.AppendAllText(directory + "\\" + timeN.ToString("yyyyMMddHH") + ".log", "************************" +
                    timeN.ToString("yyyy-MM-dd HH:mm:ss") + "***************************\r\n" + message + "\r\n");
            }
            catch (Exception ex)
            {
                PrintLog(ex.ToString());
            }
        }
       
    }

    public enum LogType
    {
        Error,
        SQL,
        Exception,
        Data,
        OfflineData,
        RunRecord

    }
}
